import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {FILES_API, DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSIONS} from './constants';
import {hasPermissions} from './utils';
import routes from './routes';
import {injectIntl} from './intl';
import {PDICheckResult, PDIOrderStatus} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import styles from './style.css';
import {getDetail, abandonOrder, approveOrder} from './api';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
const {Description} = DescriptionList;
export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onClickApproveBtn = () => {
        this.setState({loading: true});
        approveOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data} = this.state;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSIONS.update) && hasPermissions(data.options, PERMISSIONS.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSIONS.abandon) && hasPermissions(data.options, PERMISSIONS.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandonBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        if(hasPermissions(this.props.permissions, PERMISSIONS.approve) && hasPermissions(data.options, PERMISSIONS.approve))
            operations.push(<WrappedPopconfirm
                key="approve"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickApproveBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.approve',
                    defaultMessage: '是否确认审核？'
                })}>
                <Button key="approve" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.approve',
                        defaultMessage: '审核'
                    })
                }</Button>
            </WrappedPopconfirm>);
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: 'PDI检查单详情'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '编号'
                        })}>{data.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.branchName',
                            defaultMessage: '营销分公司名称'
                        })}>{data.branchName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{data.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.vin',
                            defaultMessage: 'VIN'
                        })}>{data.vin}</Description>
                        {/* <Description term={formatMessage({
                            id: 'detailPanel.text.productCategoryCode',
                            defaultMessage: '车型编号'
                        })}>{data.productCategoryCode}</Description> */}
                        <Description term={formatMessage({
                            id: 'detailPanel.text.PdiCheckResult',
                            defaultMessage: '检查结果'
                        })}>{conventEnumValueToString(PDICheckResult, data.pdiCheckResult)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.checkMan',
                            defaultMessage: '检查人姓名'
                        })}>{data.checkMan}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.checkTime',
                            defaultMessage: '检查时间'
                        })}>{formatDateTime(data.checkTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.status',
                            defaultMessage: '状态'
                        })}>{conventEnumValueToString(PDIOrderStatus, data.status)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.creatorName',
                            defaultMessage: '创建人姓名'
                        })}>{data.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.createTime',
                            defaultMessage: '创建时间'
                        })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonerName',
                            defaultMessage: '作废人姓名 '
                        })}>{data.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(data.abandonTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{data.remark}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    {data.attachments && data.attachments.length >= 0
                        ? <DescriptionList title={formatMessage({
                            id: 'detailPanel.card.attachment',
                            defaultMessage: '附件'
                        })}>
                            {data.attachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList>
                        : <DescriptionList title={formatMessage({
                            id: 'detailPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList>}
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    handleReceiveClick: () => {
        props.history.push(routes.receive.format(props.id));
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
